/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: mp_demux.h
 *
 * Purpose: the demux component for micro player
 *
 * Developer:
 *   wen.gu , 2018-09-04
 *
 * TODO: 
 *
 ***************************************************************************/
#ifndef __MP_DEMUX_H__
#define __MP_DEMUX_H__

/******************************************************************************
**    INCLUDES
******************************************************************************/
#include <functional>

#include "mp_component.h"
#include "mp_media.h"
#include "mp_data_source.h"
#include "mp_buffer.h"
/******************************************************************************
**    MACROS
******************************************************************************/
#define MP_DEMUX_COMP_TYPE "mp_demux"

/** binding member function of class */
#define MAKE_DEMUX_ADDTRACK(method) \
        std::bind(&method, this, std::placeholders::_1)


#define MAKE_DEMUX_ADDTRACK2(method, obj) \
        std::bind(&method, obj, std::placeholders::_1)



/** binding non member function(or free function) */
#define MAKE_DEMUX_ADDTRACK3(method) \
            std::bind(&method, std::placeholders::_1)

/******************************************************************************
**    TYPE DEFINITIONS
******************************************************************************/

/** the callback function to receive the track information found by demuxer */
typedef std::function<MPError(const MPTrackInfo& info)> MPDemuxAddTrack;

/** the parameter for create a demux component instance */
struct MPDemuxCreateParam: public MPCCreateParam
{
    /** container format, use to check support, get by sniffer component or API */
    eContainerFormat containerFmt = eContainerFormat::Unknown; 

    /**
     * [optional]file uri, when need read data
     * by demux-self
     * (for source_demux component(the demuxer can read data by itself))
     */
    const char* uri = nullptr;
    /**
     * [optional] source component handle, when read
     * data by MPDataSource(for pure demux component)
     */
    MPDataSource* source = nullptr;

    /**
     *@brief  add one track
     *
     *@param info  [in] track information
     *
     *@retuen success: MP_OK, fail: error code.
     *@see
     *@note the track information must include program id and track id
     */
    MPDemuxAddTrack addTrack = nullptr;
    EXTEND_MPCCREATEPARAM(MPDemuxCreateParam, MP_DEMUX_COMP_TYPE);
};

/******************************************************************************
**    CLASSES/FUNCTIONS DEFINITIONS
******************************************************************************/



class MPDemux: public MPComponent
{
public:
    MPDemux(const std::string& name, const std::string& version)
        :MPComponent(name, version)
    {
        /** todo something */
    }

    virtual ~MPDemux()
    {
        /** todo something */
    }
public:
    virtual MPError getContainerInfo(MPContainerInfo& info) = 0;
    virtual MPError read(MPBuffer** bufer) = 0;
    virtual MPError selectTrack(eMediaCategory category, int32_t idx) = 0;
    virtual MPError unsleectTrack(eMediaCategory category, int32_t idx) = 0;
    virtual MPError seek(int64_t posMs, eSeekMode mode = eSeekMode::PreviousSync) = 0;    
};


#endif /** !__MP_DEMUX_H__*/
